package main.java.org.example;

import java.awt.*;
import java.awt.event.KeyEvent;

public class Player extends GameObject {
    private int dx, dy;
    private int currentWeapon;

    private Color weaponColor;

    public Player(int x, int y, int size) {
        super(x, y, size, Color.BLUE); // 默认颜色为蓝色
        dx = 0;
        dy = 0;
        currentWeapon = 0; // 默认使用武器1
        weaponColor = Color.BLUE; // 默认武器1的颜色为蓝色
    }

    @Override
    public void draw(Graphics g) {
        g.setColor(weaponColor);
        g.fillRect(x, y, size, size);
    }

    // 移动玩家
    public void move() {
        x = Math.max(0, Math.min(x + dx, ShootingGame.WIDTH - size));
        y = Math.max(0, Math.min(y + dy, ShootingGame.HEIGHT - size));
    }

    // 获取当前武器类型
    public int getCurrentWeapon() {
        return currentWeapon;
    }

    // 键盘按下事件处理
    public void keyPressed(int keyCode) {
        switch (keyCode) {
            case KeyEvent.VK_W:
                dy = -5;
                break;
            case KeyEvent.VK_S:
                dy = 5;
                break;
            case KeyEvent.VK_A:
                dx = -5;
                break;
            case KeyEvent.VK_D:
                dx = 5;
                break;
            case KeyEvent.VK_UP:
                fireBullet(0, -1); // 向上发射子弹
                break;
            case KeyEvent.VK_DOWN:
                fireBullet(0, 1); // 向下发射子弹
                break;
            case KeyEvent.VK_LEFT:
                fireBullet(-1, 0); // 向左发射子弹
                break;
            case KeyEvent.VK_RIGHT:
                fireBullet(1, 0); // 向右发射子弹
                break;
            case KeyEvent.VK_Q:
                switchWeapon();; // 切换武器
                break;
        }
    }

    // 键盘释放事件处理
    public void keyReleased(int keyCode) {
        if (keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_S) {
            dy = 0;
        } else if (keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_D) {
            dx = 0;
        }
    }

    // 切换武器
    private void switchWeapon() {
        currentWeapon = (currentWeapon + 1) % 3; // 切换到下一个武器
        switch (currentWeapon) {
            case 0:
                weaponColor = Color.BLUE; // 武器1为蓝色
                break;
            case 1:
                weaponColor = Color.YELLOW; // 武器2为黄色
                break;
            case 2:
                weaponColor = Color.GREEN; // 武器3为绿色
                break;
        }
    }

    // 发射子弹
    private void fireBullet(int bulletDx, int bulletDy) {
        int bulletX = x + size / 2 - ShootingGame.BULLET_SIZE / 2;
        int bulletY = y + size / 2 - ShootingGame.BULLET_SIZE / 2;

        switch (currentWeapon) {
            case 0: // 武器1: 每次发射一发子弹，速度为10
                ShootingGame.bullets.add(new Bullet(bulletX, bulletY, bulletDx * 4, bulletDy * 4));
                break;
            case 1: // 武器2: 每次发射两发子弹，速度为5
                ShootingGame.bullets.add(new Bullet(bulletX, bulletY, bulletDx * 2, bulletDy * 2));
                ShootingGame.bullets.add(new Bullet(bulletX, bulletY, bulletDx * 2, bulletDy * 2));
                break;
            case 2: // 武器3: 每次发射三发子弹，速度为2
                ShootingGame.bullets.add(new Bullet(bulletX, bulletY, bulletDx, bulletDy));
                ShootingGame.bullets.add(new Bullet(bulletX, bulletY, bulletDx, bulletDy));
                ShootingGame.bullets.add(new Bullet(bulletX, bulletY, bulletDx, bulletDy));
                break;
        }
    }
}

